﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TigerAndGoats.Control;
using TigerAndGoats.Core;
using TigerAndGoats.Framework;
using TigerAndGoats.Sprites;

namespace TigerAndGoats
{
    public partial class Main : FormBase
    {
        private Point _mouseDownLocation;
        private bool _isMouseDown;

        private ChequerGraph _entireGraph;

        public Main()
        {
            InitializeComponent();
            _entireGraph = ChequerGraph.GetInstance(() => {
                this.Refresh();
            });

            GameControl.Main = this;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            // 绘制棋盘
            _entireGraph.DrawChequer(e);

            if (GameControl.IsChequerInitted)
            {
                // 绘制棋子
                foreach (var graphNode in _entireGraph.graphNodes)
                {
                    graphNode.Value.DrawPiece(e.Graphics);
                } 
            }

            // 绘制初始棋子
            if (!GameControl.IsChequerInitted)
            {
                // 清空棋子
                foreach (var graphNode in _entireGraph.graphNodes)
                {
                    graphNode.Value.SetPieceEmpty(); // 置空以后原来的棋子自动失去引用，应该对象会被自动回收的
                }

                // -->绘制老虎
                GameControl.TigerA = new Tiger(); // 将老虎单独控制（因为需要预判老虎是否无路可走，从而确定羊的胜利）
                _entireGraph.graphNodes["2,0"].DrawPiece(e.Graphics, GameControl.TigerA);
                // -->绘制羊
                _entireGraph.graphNodes["1,1"].DrawPiece(e.Graphics, new Goat());
                _entireGraph.graphNodes["1,2"].DrawPiece(e.Graphics, new Goat());
                _entireGraph.graphNodes["1,3"].DrawPiece(e.Graphics, new Goat());
                _entireGraph.graphNodes["2,1"].DrawPiece(e.Graphics, new Goat());
                _entireGraph.graphNodes["2,2"].DrawPiece(e.Graphics, new Goat());
                _entireGraph.graphNodes["2,3"].DrawPiece(e.Graphics, new Goat());
                _entireGraph.graphNodes["3,1"].DrawPiece(e.Graphics, new Goat());
                _entireGraph.graphNodes["3,2"].DrawPiece(e.Graphics, new Goat());
                _entireGraph.graphNodes["3,3"].DrawPiece(e.Graphics, new Goat());

                GameControl.IsChequerInitted = true;
            }
        }

        private void Main_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button.Equals(MouseButtons.Left))
            {
                _entireGraph.ChequerClickDetect(
                    new Point(
                        e.X - ChequerGraph.GetLeftTopPoint().X
                        , e.Y - ChequerGraph.GetLeftTopPoint().Y)
                    );
            }

            // 鼠标右键移动窗体
            if (e.Button.Equals(MouseButtons.Right))
            {
                // 记录当前鼠标的位置
                _mouseDownLocation = e.Location;
                _isMouseDown = true; 
            }
        }

        private void Main_MouseMove(object sender, MouseEventArgs e)
        {
            // 鼠标右键移动窗体
            if (e.Button.Equals(MouseButtons.Right))
            {
                if (_isMouseDown)
                {
                    // 移动当前窗体
                    this.Location = new Point(this.Location.X + (e.Location.X - _mouseDownLocation.X),
                        this.Location.Y + (e.Location.Y - _mouseDownLocation.Y));
                } 
            }
        }

        private void Main_MouseUp(object sender, MouseEventArgs e)
        {
            // 鼠标右键移动窗体
            if (e.Button.Equals(MouseButtons.Right))
            {
                _isMouseDown = false; 
            }
        }

        private void Main_Load(object sender, EventArgs e)
        {
            GameControl.InitGame();
        }
    }
}
